package com.hliushi.netty.server.handler;

import com.hliushi.netty.message.GroupMembersRequestMessage;
import com.hliushi.netty.message.GroupMembersResponseMessage;
import com.hliushi.netty.server.session.GroupSession;
import com.hliushi.netty.server.session.GroupSessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Set;

/**
 * @author llhuang10
 * @date 2021/10/22 10:59
 */
@ChannelHandler.Sharable
public class GroupMembersRequestMessageHandler extends SimpleChannelInboundHandler<GroupMembersRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupMembersRequestMessage msg) throws Exception {
        String groupName = msg.getGroupName();
        GroupSession groupSession = GroupSessionFactory.getGroupSession();
        Set<String> members = groupSession.getMembers(groupName);

        if (members.size() > 0) {
            ctx.writeAndFlush(new GroupMembersResponseMessage(members));
        }

    }
}
